package club.babyf.springai.controller;

import club.babyf.lbs.entity.amap.req.GeoCodeGeoParameterDto;
import club.babyf.lbs.entity.amap.req.WeatherInfoParameterDto;
import club.babyf.lbs.entity.amap.res.GeoCodeGeoDto;
import club.babyf.lbs.entity.amap.res.WeatherInfoDto;
import club.babyf.lbs.service.LbsService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.time.Duration;

/**
 * TestController.
 *
 * @author zhanglei.
 * @date 2025/1/10 17:55.
 * @description 测试控制器.
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/spring-ai/test")
public class TestController {

    private final LbsService lbsService;

    @GetMapping("/geo-code-geo")
    public GeoCodeGeoDto geoCodeGeo(@RequestParam("address") String address) {
        GeoCodeGeoParameterDto geoCodeGeoParameterDto = new GeoCodeGeoParameterDto();
        geoCodeGeoParameterDto.setAddress(address);
        return lbsService.geoCodeGeo(geoCodeGeoParameterDto);
    }

    @GetMapping("/weather-info")
    public WeatherInfoDto weatherInfo(@RequestParam("city") String city) {
        WeatherInfoParameterDto weatherInfoParameterDto = new WeatherInfoParameterDto();
        weatherInfoParameterDto.setCity(city);
        return lbsService.weatherInfo(weatherInfoParameterDto);
    }

    /**
     * 如果你需要浏览器逐步接收并展示数据，推荐使用 SSE 或 分块传输。
     * 直接用浏览器访问 /flux，而不修改代码或配置，通常会看到 A, B, C 一起返回，这是浏览器默认的 HTTP 响应缓冲行为。
     *
     * @return Flux<String>
     */
    @GetMapping(value = "/flux", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> flux() {
        return Flux.just("A", "B", "C").delayElements(Duration.ofMillis(500));
    }

}
